package com.xinou.daily_report.controller;


import com.alibaba.excel.EasyExcel;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.daily_report.PO.NotificationPO;
import com.xinou.daily_report.VO.ExcelNotificationVO;
import com.xinou.daily_report.VO.WeekPlanExcelVO;
import com.xinou.daily_report.entity.SysNotification;
import com.xinou.daily_report.mapper.SysNotificationMapper;
import com.xinou.daily_report.service.SysNotificationService;
import com.xinou.service.file.service.FileUploadService;
import com.xinou.sso.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author lizhongyuan
 */
@RestController
@RequestMapping("/admin/notificationDownload/")
@Api(tags = {"后台 通报 服务"})
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DailySysNotificationController {
    public static final String UploadFileUrl="https://image.xiaokesi.com/";
    private final FileUploadService fileUploadService;
    private final SysNotificationMapper sysNotificationMapper;

    @ApiOperation(value = "通报信息下载")
    @PostMapping("notificationDown")
    @RequiresPermissions({"admin:notificationDownload:notificationDown"})
    @ApiOperationSupport(ignoreParameters = {"po.id"})
    public ApiResponse<String> userListDown(@RequestBody NotificationPO po) throws IOException {
        List<ExcelNotificationVO> vos = sysNotificationMapper.notificationDown(po);
        File tempFile = File.createTempFile(String.valueOf(System.nanoTime()),".xlsx");
        EasyExcel.write(tempFile, ExcelNotificationVO.class).sheet("通报信息").doWrite(vos);
        String s = fileUploadService.fileUpload(tempFile).orElseThrow(() -> new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "下载周计划失败"));
        return new ApiResponse<>(UploadFileUrl + s);
    }

}
